<?php
/**
 * 配置分组控制器
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Page;
class ConfigGroupController extends CommonController{
	private $msg=array();
	private $module=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=M('ConfigGroup');
		$this->module=array('Home'=>L('text_module_home'),'Manage'=>L('text_module_manage'),'Common'=>L('text_module_common'));
	}
	/**
	 * 列表
	 */
	public function index(){
		//判断授权
		if(!$this->checkPermission('ConfigGroup')) $this->jump();
		$count=$this->Model->count();
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$list=$this->Model->order('sort_order asc')->limit($Page->firstRow,$Page->listRows)->select();
		$language_id=get_default_lng_id();
		foreach($list as $k=>$v){
			$text=json_decode($v['text'],true);
			$list[$k]['title']=isset($text[$language_id]['title'])?$text[$language_id]['title']:'';
		}
		$show=$Page->show();
		$url['p']=I('p','');
		$this->assign('modules',$this->module);
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();
	}
	/**
	 * 新增/更新
	 */
	public function oper($config_group_id=0){
		//验证授权
		if(!$config_group_id && !$this->checkPermission('ConfigGroupAdd')) $this->jump;
		$config_group=array();
		//如果传入了$config_group_id则是编辑
		if($config_group_id){
			$map=array('config_group_id'=>$config_group_id);
			$config_group=$this->Model->where($map)->find();
			//如果查询不到启示录，则说明config_group_id是错误的，转到404
			if(empty($config_group)) $this->jump(L('error_404'));
			//处理配置分组的text字段json转为数组，便于输出
			$config_group['text']=json_decode($config_group['text'],true);
		}
		if(IS_POST){
			//如果是post的请求，则说明用户是在保存数据
			$config_group=I('post.');
			if($this->validateForm()){
				//将text字段的内容转为json便于保存
				$config_group['text']=json_encode($config_group['text']);
				if($config_group_id){
					$this->Model->where($map)->save($config_group);
				}else{
					$this->Model->add($config_group);
				}
				session('success',$config_group_id?L('success_edit'):L('success_add'));
				//获取分页，操作完成，回来列表页面
				$url['p']=I('get.p','');
				$this->redirect('index',$url);
			}else{
				//如果存在$this->msg['warning']说明权限验证没有通过，msg.warning用于输出权限验证未通过，否则才是要保存的数据未通过合法性验证
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$this->assign('languages',get_langs());
		$this->assign('config_group',$config_group);
		$this->assign('msg',$this->msg);
		$this->assign('modules',$this->module);
		$this->display();
	}
	/**
	 * 数据验证
	 */
	private function validateForm(){
		$config_group_id=I('config_group_id',0);
		if($config_group_id && !$this->checkPermission('ConfigGroupEdit')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach($data['text'] as $language_id => $v){
			if(mb_strlen($v['title'])<2 || mb_strlen($v['title'])>50) $this->msg['title'][$language_id]=L('error_title');
		}
		if(mb_strlen($data['module'])<2 || mb_strlen($data['module'])>50) $this->msg['module']=L('error_module');
		return !$this->msg;
	}
	/**
	 * 删除
	 */
	public function del(){
		if(!$this->checkPermission('ConfigGroupDelete')){
			session('danger',L('error_not_permission'));
		}else{
			$ids=I('post.selected');
			$map=array('config_group_id'=>array('in',$ids));
			$config=M('Config')->where($map)->find();
			if(!empty($config)) {
				session('danger',L('error_have_config'));
			}else{
				$count=0;
				$count=$this->Model->where($map)->delete();
				session('success',L('success_delete',array('count'=>$count)));
			}
		}
		$url['p']=I('get.p');
		$this->redirect('index',$url);
	}
}